package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.SubType;
import ex.com.jm.food.service.AdminMenuService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
/**
 * 
 * @author zhaojw
 *
 */
public class AdminMenuAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Menu menu;
	private AdminMenuService adminMenuService;
	private String menuName;
	private String order;
	private String sort;
	private Integer type;
	private Integer status;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private String menuId;
	private Integer subId;
	private Integer supId;
	private List<SubType> subTypes;
	private Map<String, String> dateMap;
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public AdminMenuService getAdminMenuService() {
		return adminMenuService;
	}
	public void setAdminMenuService(AdminMenuService adminMenuService) {
		this.adminMenuService = adminMenuService;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public Integer getSupId() {
		return supId;
	}
	public void setSupId(Integer supId) {
		this.supId = supId;
	}
	public List<SubType> getSubTypes() {
		return subTypes;
	}
	public void setSubTypes(List<SubType> subTypes) {
		this.subTypes = subTypes;
	}
	public Integer getSubId() {
		return subId;
	}
	public void setSubId(Integer subId) {
		this.subId = subId;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达菜单管理页面
	 * @return
	 */
	public String menuList(){
		dateMap=DateUtils.getDate();
		return "menuList";
	}
	/**
	 * 获取菜单数据，返回json
	 */
	public void getMenus(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 int offset=(initPage-1)*pageRow;
		 List<Menu> menus=this.getAdminMenuService().getMenus(offset, pageRow, menuName, type, status,subId,supId,s_date, e_date, sort, order);
		 JSONArray jsonmenu=JSONArray.fromObject(menus);
		 totalCount=this.getAdminMenuService().menuCount(menuName, type, status,subId,supId,s_date, e_date);
		 JSONObject jo=JsonUtils.buildJson(jsonmenu, initPage, pageRow, totalCount);
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(jo);
			out.print(jo);
			out.flush();
			out.close();
	}
	/**
	 * 获取菜单信息/到达编辑页面
	 * @return
	 */
	public String editMenu(){
		subTypes=this.getAdminMenuService().getSubTypes();
		if(menuId!=null&&!menuId.equals("")){
			menu=this.getAdminMenuService().editMenu(menuId);
			//System.out.println(menu.getMenuImageg());
			
		}
		return "edit";
	}
	/**
	 * 保存/更新菜单
	 * @return
	 */
	public String saveMenu(){
		if(menu.getMenuName()==null||menu.getMenuName().equals("")){
			addFieldError("menu.menuName", "菜单名称不能为空");
			return "edit";
		}else if (menu.getMenuImageg()==null||menu.getMenuImageg().equals("")) {
			addFieldError("menu.menuImage", "菜单图片不能为空");
			return "edit";
		}else if (menu.getMenuPrice()==null||menu.getMenuPrice().equals("")) {
			addFieldError("menu.menuPrice", "菜单价格不能为空");
			return "edit";
		}
		if(menu.getMenuId()!=null&&!menu.getMenuId().equals("")){
			menu.setMenuInDate(DateUtils.getCurrentDate());
			this.getAdminMenuService().update(menu);
			return "menuList";
		}
		this.getAdminMenuService().saveMenu(menu);
 		return "menuList";
		
	}
	/**
	 * 更新菜单的类型
	 * @return
	 */
	public String updateType(){
		this.getAdminMenuService().updateTypea(menuId, type);
		return "menuList";
		
	}
	/**
	 * 更新菜单的状态
	 * @return
	 */
	public String updateStatus(){
		this.getAdminMenuService().updateStatus(menuId, status);
		return "menuList";
	}
}
